
#include "Beam_Quality_Result.h"
#include "ui_Beam_Quality_Result.h"
#include "../RayCi/Beam_Quality.h"
#include "BeamQualityTypes.h"

Beam_Quality_Result::Beam_Quality_Result(QWidget *parent) :
    QWidget(parent), ui(new Ui::Beam_Quality_Result) {
    ui->setupUi(this);
    setupTreeWidget();

    // Current Plane
    createTopLevelSection(0, "Current Plane", oneTopItem, {
        {NumberItem, {"Number", " ", "0", " ", " "}},
        {CentroidXItem, {"Centroid", "X", "0.000", " ", " ", "mm"}},
        {CentroidYItem, {"Centroid", "Y", "0.000", " ", " ", "mm"}},
        {PositionZItem, {"Z Position", "", "0.000", " ", " ", "mm"}},
        {WidthGeneralizedItem, {"Width Generalized", " ", "-", " ", " ", "mm"}},
        {WidthMajorItem, {"Width Major", " ", "-", " ", " ", "mm"}},
        {WidthMinorItem, {"Width Minor", "kappa", "-", " ", " ", "mm"}},
        {AzimuthAngleItem, {"Azimuth Angle", "4 sigma", "-", " ", " ", " ° "}},
        {EllipticityItem, {"Ellipticity", "4 sigma", "-", " ", " ", ""}},
        {EccentricityItem, {"Eccentricity", "4 sigma", "-", " ", " ", ""}}
    });
    oneTop = ui->treeWidget->topLevelItem(0);

    // General
    createTopLevelSection(1, "General", twoTopItem, {
        {NumberofPositionsItem, {"Number of Positions", " ", "0", " ", " ", " "}},
        {GeometricBeamTypeItem, {"Geometric Beam Type", " ", "probably stigmatic", " ", " "}},
        {ResultsItem, {"Results", " ", "after lens", " ", " ", "mm"}}
    });
    twoTop = ui->treeWidget->topLevelItem(1);

    // Torsion
    createTopLevelSection(2, "Torsion", threeTopItem, {
        {N_ellipticalItem, {"N_elliptical", " ", "0", " ", " ", " "}},
        {TorsionItem, {"Torsion", " ", "-", " ", " ° "}}
    });
    threeTop = ui->treeWidget->topLevelItem(2);

    // Caustic U
    createTopLevelSection(3, "Caustic U", fourTopItem, {
        {uISO11146_1Item, {"ISO 11146-1", " ", "Not conform:The fit could not be performed", " ", " ", " "}},
        {uCausticPositionItem, {"Caustic Position", "z0", "(The fit could not be performed)", " ", "mm"}},
        {uBeamWaistDiameterItem, {"Beam Waist Diameter", "d0", "(The fit could not be performed)", " ", "mm"}},
        {uRayleighLengthItem, {"Rayleigh Length", "zR", "(The fit could not be performed)", " ", "mm"}},
        {uDivergenceItem, {"Divergence", "theta", "(The fit could not be performed)", " ", "mrad"}},
        {uBeamParameterProductItem, {"Beam Parameter Product", "BPP", "-", " ", "mm mrad"}},
        {uBeamPropagationRatioItem, {"Beam Propagation Ratio", "M²", "-", " ", " "}},
        {uBeamPropagationFactorItem, {"Beam Propagation Factor", "K", "-", " ", " "}}
    });
    fourTop = ui->treeWidget->topLevelItem(3);

    // Caustic V
    createTopLevelSection(4, "Caustic V", fiveTopItem, {
        {vISO11146_1Item, {"ISO 11146-1", " ", "Not conform:The fit could not be performed", " ", " ", " "}},
        {vCausticPositionItem, {"Caustic Position", "z0", "(The fit could not be performed)", " ", "mm"}},
        {vBeamWaistDiameterItem, {"Beam Waist Diameter", "d0", "(The fit could not be performed)", " ", "mm"}},
        {vRayleighLengthItem, {"Rayleigh Length", "zR", "(The fit could not be performed)", " ", "mm"}},
        {vDivergenceItem, {"Divergence", "theta", "(The fit could not be performed)", " ", "mrad"}},
        {vBeamParameterProductItem, {"Beam Parameter Product", "BPP", "-", " ", "mm mrad"}},
        {vBeamPropagationRatioItem, {"Beam Propagation Ratio", "M²", "-", " ", " "}},
        {vBeamPropagationFactorItem, {"Beam Propagation Factor", "K", "-", " ", " "}}
    });
    fiveTop = ui->treeWidget->topLevelItem(4);

    // Caustic UV
    createTopLevelSection(5, "Caustic UV", sixTopItem, {
        {AstigmatismRelItem, {"Astigmatism", "Ast", "(The fit could not be performed)", " ", " ", " % "}},
        {AsymmetryItem, {"Asymmetry", "Asy", "(The fit could not be performed)", " ", " ", " "}}
    });
    sixTop = ui->treeWidget->topLevelItem(5);

    // Caustic Fit
    createTopLevelSection(6, "Caustic Fit", sevenTopItem, {
        {CausticPositionItem, {"Caustic Position", "z0", "(The fit could not be performed)", " ", " ", " mm "}},
        {BeamWaistDiameterItem, {"Beam Waist Diameter", "d0", "(The fit could not be performed)", " ", " ", " mm "}},
        {ReyleighLengthItem, {"ReyleighLength", "zR", "(The fit could not be performed)", " ", " ", " mm "}},
        {DivergenceItem, {"Divergence", "theta", "(The fit could not be performed)", " ", " ", " mrad "}}
    });
    sevenTop = ui->treeWidget->topLevelItem(6);

    // Caustic 3D Fit
    createTopLevelSection(7, "Caustic 3D Fit", eightTopItem, {
        {ISO11146_2Item, {"ISO 11146-2", "", "(Not conform:The fit could not be performed)", " ", " ", "  "}},
        {ReyleighLengthleftItem, {"Reyleigh Length ", "zR left", "(Not enough data points)", " ", " ", " mm "}},
        {ReyleighLengthrightItem, {"Reyleigh Length", "zR right", "(Not enough data points)", " ", " ", " mm "}},
        {TwistItem, {"Twist", "", "(Assumed to be Zero)", " ", " ", "  "}},
        {AstigmatismrelItem, {"Astigmatism rel", "Ast", "-", " ", " ", " % "}},
        {AstigmatismangleItem, {"Astigmatism Angle", "Ast Angle", "-", " ", " ", " ° "}},
        {Divergence3DItem, {"Divergence", "Omega", "(Not enough data points)", " ", " ", " usr "}},
        {BeamParameterProductItem, {"Beam Parameter Product", "BPP", "(The fit could not be performed)", " ", " ", " mm mrad "}},
        {BeamPropagationRatioItem, {"Beam Propagation Ratio", "M²eff", "(The fit could not be performed)", " ", " ", "  "}},
        {BeamPropagationFactorItem, {"Beam Propagation Factor", "K", "(The fit could not be performed)", " ", " ", "  "}},
        {IntrinsicAstigmatismItem, {"Intrinsic Astigmatism", "a", "(The fit could not be performed)", " ", " ", "  "}}
    });
    eightTop = ui->treeWidget->topLevelItem(7);

    // Summary
    createTopLevelSection(8, "Summary", nineTopItem, {
        {ISO11146_2_SummaryItem, {"ISO 11146-2", "", "(Not conform:The fit could not be performed)", " ", " ", "  "}},
        {CausticPosition_SummaryItem, {"Caustic Position", "z0", "(The fit could not be performed)", " ", " ", " mm "}},
        {BeamWaistDiameter_SummaryItem, {"Beam Waist Diameter", "d0", "(The fit could not be performed)", " ", " ", " mm "}},
        {ReyleighLength_SummaryItem, {"ReyleighLength", "zR", "(The fit could not be performed)", " ", " ", " mm "}},
        {Divergence_SummaryItem, {"Divergence", "theta", "(The fit could not be performed)", " ", " ", " mrad "}},
        {Twist_SummaryItem, {"Twist", "", "(Assumed to be Zero)", " ", " ", "  "}},
        {Astigmatismrel_SummaryItem, {"Astigmatism rel", "Ast", "-", " ", " ", " % "}},
        {Astigmatismangle_SummaryItem, {"Astigmatism Angle", "Ast Angle", "-", " ", " ", " ° "}},
        {Divergence3D_SummaryItem, {"Divergence", "Omega", "(Not enough data points)", " ", " ", " usr "}},
        {BeamParameterProduct_SummaryItem, {"Beam Parameter Product", "BPP", "(The fit could not be performed)", " ", " ", " mm mrad "}},
        {BeamPropagationRatio_SummaryItem, {"Beam Propagation Ratio", "M²eff", "(The fit could not be performed)", " ", " ", "  "}},
        {BeamPropagationFactor_SummaryItem, {"Beam Propagation Factor", "K", "(The fit could not be performed)", " ", " ", "  "}},
        {IntrinsicAstigmatism_SummaryItem, {"Intrinsic Astigmatism", "a", "(The fit could not be performed)", " ", " ", "  "}}
    });
    nineTop = ui->treeWidget->topLevelItem(8);



    sevenTopItem->setHidden(true);
    eightTopItem->setHidden(true);
    nineTopItem->setHidden(true);
    ui->treeWidget->expandAll();
}

bool Beam_Quality_Result::isDouble(const QString& str) {
    bool ok;
    str.toDouble(&ok);
    return ok;
}



inline void Beam_Quality_Result::updateItemWithUncertainty(
    QTreeWidgetItem* item,
    const QString& value,
    const QString& uncertainty
) {
    item->setText(COLUMN_VALUE, value);
    if (isDouble(value)) {
        item->setText(COLUMN_UNCERTAINTY_SYMBOL, "+/-");
        item->setText(COLUMN_UNCERTAINTY_VALUE, uncertainty);
    }
}

void Beam_Quality_Result::setupTreeWidget() {
    ui->treeWidget->setColumnCount(6);
    ui->treeWidget->setHeaderLabels(QStringList() << "Parameter" << " " << "Value" << " " << "Uncertainty" << "Unit");
    ui->treeWidget->setColumnWidth(COLUMN_PARAMETER, 200);
    ui->treeWidget->setColumnWidth(COLUMN_SYMBOL, 30);
}

void Beam_Quality_Result::createTopLevelSection(int index, const QString& title,
                                              QTreeWidgetItem*& topItem,
                                              const std::vector<TreeItemData<QStringList>>& children) {
    topItem = new QTreeWidgetItem(QStringList{title});
    ui->treeWidget->addTopLevelItem(topItem);
    topItem->setFirstColumnSpanned(true);
    topItem->setSizeHint(0, QSize(0, 30));
    addChildItems(ui->treeWidget->topLevelItem(index), children);
}

void Beam_Quality_Result::addChildItems(QTreeWidgetItem* parent,
                                      const std::vector<TreeItemData<QStringList>>& children) {
    for (const auto& child : children) {
        child.item = new QTreeWidgetItem(child.data);
        parent->addChild(child.item);
    }
}




void Beam_Quality_Result::updateCausticParams(
    QTreeWidgetItem* isoItem,
    QTreeWidgetItem* positionItem,
    QTreeWidgetItem* waistItem,
    QTreeWidgetItem* rayleighItem,
    QTreeWidgetItem* divergenceItem,
    QTreeWidgetItem* bppItem,
    QTreeWidgetItem* ratioItem,
    QTreeWidgetItem* factorItem,
    const CausticParamsBase& params
) {
    isoItem->setText(COLUMN_VALUE, params.ISO11146_1);
    updateItemWithUncertainty(positionItem, params.CausticPosition, params.CausticPosition_Uncertainty);
    updateItemWithUncertainty(waistItem, params.BeamWaistDiameter, params.BeamWaistDiameter_Uncertainty);
    updateItemWithUncertainty(rayleighItem, params.RayleighLength, params.RayleighLength_Uncertainty);
    updateItemWithUncertainty(divergenceItem, params.Divergence, params.Divergence_Uncertainty);
    updateItemWithUncertainty(bppItem, params.BeamParameterProduct, params.BeamParameterProduct_Uncertainty);
    updateItemWithUncertainty(ratioItem, params.BeamPropagationRatio, params.BeamPropagationRatio_Uncertainty);
    updateItemWithUncertainty(factorItem, params.BeamPropagationFactor, params.BeamPropagationFactor_Uncertainty);
}

void Beam_Quality_Result::clear() {
    // Current Plane
    NumberItem->setText(COLUMN_VALUE, "0");
    NumberItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    NumberItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    CentroidXItem->setText(COLUMN_VALUE, "0.000");
    CentroidXItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    CentroidXItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    CentroidYItem->setText(COLUMN_VALUE, "0.000");
    CentroidYItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    CentroidYItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    PositionZItem->setText(COLUMN_VALUE, "0.000");
    PositionZItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    PositionZItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    WidthGeneralizedItem->setText(COLUMN_VALUE, "-");
    WidthGeneralizedItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    WidthGeneralizedItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    WidthMajorItem->setText(COLUMN_VALUE, "-");
    WidthMajorItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    WidthMajorItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    WidthMinorItem->setText(COLUMN_VALUE, "-");
    WidthMinorItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    WidthMinorItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    AzimuthAngleItem->setText(COLUMN_VALUE, "-");
    AzimuthAngleItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    AzimuthAngleItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    EllipticityItem->setText(COLUMN_VALUE, "-");
    EllipticityItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    EllipticityItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    EccentricityItem->setText(COLUMN_VALUE, "-");
    EccentricityItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    EccentricityItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    // General
    NumberofPositionsItem->setText(COLUMN_VALUE, "0");
    NumberofPositionsItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    NumberofPositionsItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    GeometricBeamTypeItem->setText(COLUMN_VALUE, "probably stigmatic");
    GeometricBeamTypeItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    GeometricBeamTypeItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    ResultsItem->setText(COLUMN_VALUE, "after lens");
    ResultsItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    ResultsItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    // Torsion
    N_ellipticalItem->setText(COLUMN_VALUE, "0");
    N_ellipticalItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    N_ellipticalItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    TorsionItem->setText(COLUMN_VALUE, "-");
    TorsionItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    TorsionItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    // Caustic U
    uISO11146_1Item->setText(COLUMN_VALUE, "Not conform:The fit could not be performed");
    uISO11146_1Item->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    uISO11146_1Item->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    uCausticPositionItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    uCausticPositionItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    uCausticPositionItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    uBeamWaistDiameterItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    uBeamWaistDiameterItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    uBeamWaistDiameterItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    uRayleighLengthItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    uRayleighLengthItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    uRayleighLengthItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    uDivergenceItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    uDivergenceItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    uDivergenceItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    uBeamParameterProductItem->setText(COLUMN_VALUE, "-");
    uBeamParameterProductItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    uBeamParameterProductItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    uBeamPropagationRatioItem->setText(COLUMN_VALUE, "-");
    uBeamPropagationRatioItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    uBeamPropagationRatioItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    uBeamPropagationFactorItem->setText(COLUMN_VALUE, "-");
    uBeamPropagationFactorItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    uBeamPropagationFactorItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    // Caustic V
    vISO11146_1Item->setText(COLUMN_VALUE, "Not conform:The fit could not be performed");
    vISO11146_1Item->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    vISO11146_1Item->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    vCausticPositionItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    vCausticPositionItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    vCausticPositionItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    vBeamWaistDiameterItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    vBeamWaistDiameterItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    vBeamWaistDiameterItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    vRayleighLengthItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    vRayleighLengthItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    vRayleighLengthItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    vDivergenceItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    vDivergenceItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    vDivergenceItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    vBeamParameterProductItem->setText(COLUMN_VALUE, "-");
    vBeamParameterProductItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    vBeamParameterProductItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    vBeamPropagationRatioItem->setText(COLUMN_VALUE, "-");
    vBeamPropagationRatioItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    vBeamPropagationRatioItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    vBeamPropagationFactorItem->setText(COLUMN_VALUE, "-");
    vBeamPropagationFactorItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    vBeamPropagationFactorItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    // Caustic UV
    AstigmatismRelItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    AstigmatismRelItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    AstigmatismRelItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    AsymmetryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    AsymmetryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    AsymmetryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    // Caustic Fit
    CausticPositionItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    CausticPositionItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    CausticPositionItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    BeamWaistDiameterItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    BeamWaistDiameterItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    BeamWaistDiameterItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    ReyleighLengthItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    ReyleighLengthItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    ReyleighLengthItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    DivergenceItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    DivergenceItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    DivergenceItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    // Caustic 3D Fit
    ISO11146_2Item->setText(COLUMN_VALUE, "(Not conform:The fit could not be performed)");
    ISO11146_2Item->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    ISO11146_2Item->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    ReyleighLengthleftItem->setText(COLUMN_VALUE, "(Not enough data points)");
    ReyleighLengthleftItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    ReyleighLengthleftItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    ReyleighLengthrightItem->setText(COLUMN_VALUE, "(Not enough data points)");
    ReyleighLengthrightItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    ReyleighLengthrightItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    TwistItem->setText(COLUMN_VALUE, "(Assumed to be Zero)");
    TwistItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    TwistItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    AstigmatismrelItem->setText(COLUMN_VALUE, "-");
    AstigmatismrelItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    AstigmatismrelItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    AstigmatismangleItem->setText(COLUMN_VALUE, "-");
    AstigmatismangleItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    AstigmatismangleItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    Divergence3DItem->setText(COLUMN_VALUE, "(Not enough data points)");
    Divergence3DItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    Divergence3DItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    BeamParameterProductItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    BeamParameterProductItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    BeamParameterProductItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    BeamPropagationRatioItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    BeamPropagationRatioItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    BeamPropagationRatioItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    BeamPropagationFactorItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    BeamPropagationFactorItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    BeamPropagationFactorItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    IntrinsicAstigmatismItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    IntrinsicAstigmatismItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    IntrinsicAstigmatismItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    // Summary
    ISO11146_2_SummaryItem->setText(COLUMN_VALUE, "(Not conform:The fit could not be performed)");
    ISO11146_2_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    ISO11146_2_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    CausticPosition_SummaryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    CausticPosition_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    CausticPosition_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    BeamWaistDiameter_SummaryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    BeamWaistDiameter_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    BeamWaistDiameter_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    ReyleighLength_SummaryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    ReyleighLength_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    ReyleighLength_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    Divergence_SummaryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    Divergence_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    Divergence_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    Twist_SummaryItem->setText(COLUMN_VALUE, "(Assumed to be Zero)");
    Twist_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    Twist_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    Astigmatismrel_SummaryItem->setText(COLUMN_VALUE, "-");
    Astigmatismrel_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    Astigmatismrel_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    Astigmatismangle_SummaryItem->setText(COLUMN_VALUE, "-");
    Astigmatismangle_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    Astigmatismangle_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    Divergence3D_SummaryItem->setText(COLUMN_VALUE, "(Not enough data points)");
    Divergence3D_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    Divergence3D_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    BeamParameterProduct_SummaryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    BeamParameterProduct_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    BeamParameterProduct_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    BeamPropagationRatio_SummaryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    BeamPropagationRatio_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    BeamPropagationRatio_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    BeamPropagationFactor_SummaryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    BeamPropagationFactor_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    BeamPropagationFactor_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");

    IntrinsicAstigmatism_SummaryItem->setText(COLUMN_VALUE, "(The fit could not be performed)");
    IntrinsicAstigmatism_SummaryItem->setText(COLUMN_UNCERTAINTY_SYMBOL, " ");
    IntrinsicAstigmatism_SummaryItem->setText(COLUMN_UNCERTAINTY_VALUE, " ");
}



void Beam_Quality_Result::updateResult(bool Allupdate,
    const CurrentPlaneParams& currentPlane,
    const GeneralParams& general,
    const TorsionParams& torsion,
    const UCausticParams& uCaustic,
    const VCausticParams& vCaustic,
    const UVCausticParams& uvCaustic,
    const CausticFitParams& causticFit,
    const Caustic3DFitParams& caustic3DFit
) {
    // 当前平面参数更新
    if (Allupdate) {
        NumberItem->setText(COLUMN_VALUE, QString::number(currentPlane.Number));
        CentroidXItem->setText(COLUMN_VALUE, QString::number(currentPlane.CentroidX));
        CentroidYItem->setText(COLUMN_VALUE, QString::number(currentPlane.CentroidY));
        PositionZItem->setText(COLUMN_VALUE, QString::number(currentPlane.PositionZ));
        WidthGeneralizedItem->setText(COLUMN_VALUE, QString::number(currentPlane.WidthGeneralized));
        WidthMajorItem->setText(COLUMN_VALUE, QString::number(currentPlane.WidthMajor));
        WidthMinorItem->setText(COLUMN_VALUE, QString::number(currentPlane.WidthMinor));
        AzimuthAngleItem->setText(COLUMN_VALUE, QString::number(currentPlane.AzimuthAngle));
        EllipticityItem->setText(COLUMN_VALUE, QString::number(currentPlane.Ellipticity));
        EccentricityItem->setText(COLUMN_VALUE, QString::number(currentPlane.Eccentricity));
    }
    // 通用参数更新
    NumberofPositionsItem->setText(COLUMN_VALUE, QString::number(general.NumberofPositions));
    GeometricBeamTypeItem->setText(COLUMN_VALUE, general.GeometricBeamType);
    ResultsItem->setText(COLUMN_VALUE, general.Results);

    // 扭曲参数更新
    N_ellipticalItem->setText(COLUMN_VALUE, QString::number(torsion.N_elliptical));
    TorsionItem->setText(COLUMN_VALUE, QString::number(torsion.Torsion));

    // U因果关系参数更新
    updateCausticParams(
        uISO11146_1Item, uCausticPositionItem, uBeamWaistDiameterItem,
        uRayleighLengthItem, uDivergenceItem, uBeamParameterProductItem,
        uBeamPropagationRatioItem, uBeamPropagationFactorItem,
        uCaustic
    );

    // V因果关系参数更新
    updateCausticParams(
        vISO11146_1Item, vCausticPositionItem, vBeamWaistDiameterItem,
        vRayleighLengthItem, vDivergenceItem, vBeamParameterProductItem,
        vBeamPropagationRatioItem, vBeamPropagationFactorItem,
        vCaustic
    );

    // UV因果关系参数更新
    updateItemWithUncertainty(AstigmatismRelItem, uvCaustic.AstigmatismRel, uvCaustic.AstigmatismRel_Uncertainty);
    updateItemWithUncertainty(AsymmetryItem, uvCaustic.Asymmetry, uvCaustic.Asymmetry_Uncertainty);

    // Caustic Fit参数更新
    updateItemWithUncertainty(CausticPositionItem, causticFit.CausticPosition, causticFit.CausticPosition_Uncertainty);
    updateItemWithUncertainty(BeamWaistDiameterItem, causticFit.BeamWaistDiameter, causticFit.BeamWaistDiameter_Uncertainty);
    updateItemWithUncertainty(ReyleighLengthItem, causticFit.RayleighLength, causticFit.RayleighLength_Uncertainty);
    updateItemWithUncertainty(DivergenceItem, causticFit.Divergence, causticFit.Divergence_Uncertainty);

    // Caustic 3D Fit参数更新
    updateItemWithUncertainty(ISO11146_2Item, caustic3DFit.ISO11146_2, "");
    updateItemWithUncertainty(ReyleighLengthleftItem, caustic3DFit.ReyleighLengthLeft, caustic3DFit.ReyleighLengthLeft_Uncertainty);
    updateItemWithUncertainty(ReyleighLengthrightItem, caustic3DFit.ReyleighLengthRight, caustic3DFit.ReyleighLengthRight_Uncertainty);
    updateItemWithUncertainty(TwistItem, caustic3DFit.Twist, caustic3DFit.Twist_Uncertainty);
    updateItemWithUncertainty(AstigmatismrelItem, caustic3DFit.AstigmatismRel, caustic3DFit.AstigmatismRel_Uncertainty);
    updateItemWithUncertainty(AstigmatismangleItem, caustic3DFit.AstigmatismAngle, caustic3DFit.AstigmatismAngle_Uncertainty);
    updateItemWithUncertainty(Divergence3DItem, caustic3DFit.Divergence3D, caustic3DFit.Divergence3D_Uncertainty);
    updateItemWithUncertainty(BeamParameterProductItem, caustic3DFit.BeamParameterProduct, caustic3DFit.BeamParameterProduct_Uncertainty);
    updateItemWithUncertainty(BeamPropagationRatioItem, caustic3DFit.BeamPropagationRatio, caustic3DFit.BeamPropagationRatio_Uncertainty);
    updateItemWithUncertainty(BeamPropagationFactorItem, caustic3DFit.BeamPropagationFactor, caustic3DFit.BeamPropagationFactor_Uncertainty);
    updateItemWithUncertainty(IntrinsicAstigmatismItem, caustic3DFit.IntrinsicAstigmatism, caustic3DFit.IntrinsicAstigmatism_Uncertainty);

    // Summary参数更新 - 从Caustic 3D Fit获取数据
    updateItemWithUncertainty(ISO11146_2_SummaryItem, caustic3DFit.ISO11146_2, "");
    updateItemWithUncertainty(Twist_SummaryItem, caustic3DFit.Twist, caustic3DFit.Twist_Uncertainty);
    updateItemWithUncertainty(Astigmatismrel_SummaryItem, caustic3DFit.AstigmatismRel, caustic3DFit.AstigmatismRel_Uncertainty);
    updateItemWithUncertainty(Astigmatismangle_SummaryItem, caustic3DFit.AstigmatismAngle, caustic3DFit.AstigmatismAngle_Uncertainty);
    updateItemWithUncertainty(Divergence3D_SummaryItem, caustic3DFit.Divergence3D, caustic3DFit.Divergence3D_Uncertainty);
    updateItemWithUncertainty(BeamParameterProduct_SummaryItem, caustic3DFit.BeamParameterProduct, caustic3DFit.BeamParameterProduct_Uncertainty);
    updateItemWithUncertainty(BeamPropagationRatio_SummaryItem, caustic3DFit.BeamPropagationRatio, caustic3DFit.BeamPropagationRatio_Uncertainty);
    updateItemWithUncertainty(BeamPropagationFactor_SummaryItem, caustic3DFit.BeamPropagationFactor, caustic3DFit.BeamPropagationFactor_Uncertainty);
    updateItemWithUncertainty(IntrinsicAstigmatism_SummaryItem, caustic3DFit.IntrinsicAstigmatism, caustic3DFit.IntrinsicAstigmatism_Uncertainty);

    // Summary参数更新 - 从Caustic Fit获取数据
    updateItemWithUncertainty(CausticPosition_SummaryItem, causticFit.CausticPosition, causticFit.CausticPosition_Uncertainty);
    updateItemWithUncertainty(BeamWaistDiameter_SummaryItem, causticFit.BeamWaistDiameter, causticFit.BeamWaistDiameter_Uncertainty);
    updateItemWithUncertainty(ReyleighLength_SummaryItem, causticFit.RayleighLength, causticFit.RayleighLength_Uncertainty);
    updateItemWithUncertainty(Divergence_SummaryItem, causticFit.Divergence, causticFit.Divergence_Uncertainty);

}


void Beam_Quality_Result::updateResult(bool allupdate,const BeamQualityResultParams& params) {
    updateResult(allupdate,
        params.currentPlane,
        params.general,
        params.torsion,
        params.uCaustic,
        params.vCaustic,
        params.uvCaustic,
        params.causticFit,
        params.caustic3DFit
    );
}
void Beam_Quality_Result::showISO_1() {
    sevenTopItem->setHidden(true);
    eightTopItem->setHidden(true);
    nineTopItem->setHidden(true);
    ui->treeWidget->collapseAll();
}

void Beam_Quality_Result::showISO_2() {
    sevenTopItem->setHidden(false);
    eightTopItem->setHidden(false);
    nineTopItem->setHidden(true);
    ui->treeWidget->expandAll();
}

void Beam_Quality_Result::showDetails() {
    sevenTopItem->setHidden(true);
    eightTopItem->setHidden(true);
    nineTopItem->setHidden(false);
    ui->treeWidget->expandAll();
}

void Beam_Quality_Result::SetIsRawResult(bool IsRawBeam) {
    if (IsRawBeam)
        ResultsItem->setText(COLUMN_VALUE, "Raw Beam");
    else
        ResultsItem->setText(COLUMN_VALUE, "after lens");

}

void Beam_Quality_Result::updateCurrentPlane(const CurrentPlaneParams& params) {
    NumberItem->setText(COLUMN_VALUE, QString::number(params.Number));
    CentroidXItem->setText(COLUMN_VALUE, QString::number(params.CentroidX));
    CentroidYItem->setText(COLUMN_VALUE, QString::number(params.CentroidY));
    PositionZItem->setText(COLUMN_VALUE, QString::number(params.PositionZ));
    WidthGeneralizedItem->setText(COLUMN_VALUE, QString::number(params.WidthGeneralized));
    WidthMajorItem->setText(COLUMN_VALUE, QString::number(params.WidthMajor));
    WidthMinorItem->setText(COLUMN_VALUE, QString::number(params.WidthMinor));
    AzimuthAngleItem->setText(COLUMN_VALUE, QString::number(params.AzimuthAngle));
    EllipticityItem->setText(COLUMN_VALUE, QString::number(params.Ellipticity));
    EccentricityItem->setText(COLUMN_VALUE, QString::number(params.Eccentricity));

}



Beam_Quality_Result::~Beam_Quality_Result() {
    delete ui;
}
